const mongoose = require('mongoose');
const { Student, Class, Teacher } = require('./models/db');

mongoose.connect('mongodb://localhost:27017/students_management')
  .then(async () => {
    console.log('连接成功');
    
    // 测试学生populate
    const student = await Student.findOne()
      .populate({
        path: 'class',
        select: 'className academy stage advisor lecturer',
        populate: [
          { path: 'lecturer', select: 'name teacherId', model: 'teacher' },
          { path: 'advisor', select: 'name teacherId', model: 'teacher' }
        ]
      });
    
    console.log('\n=== 学生数据 ===');
    console.log('学生:', student.name);
    console.log('班级:', student.class?.className);
    console.log('讲师:', student.class?.lecturer?.name || 'null');
    console.log('导员:', student.class?.advisor?.name || 'null');
    console.log('讲师类型:', typeof student.class?.lecturer);
    console.log('导员类型:', typeof student.class?.advisor);
    
    // 测试班级数据
    const classData = await Class.findOne({ className: student.class?.className })
      .populate('lecturer', 'name')
      .populate('advisor', 'name');
    
    console.log('\n=== 班级数据 ===');
    console.log('班级:', classData.className);
    console.log('讲师:', classData.lecturer?.name || 'null');
    console.log('导员:', classData.advisor?.name || 'null');
    
    process.exit(0);
  })
  .catch(err => {
    console.error('错误:', err);
    process.exit(1);
  });

